import React, { useState, useEffect, useRef } from 'react';
import { useRequest, useModel } from 'umi';
import { Row, List, Card, Tabs, Descriptions, Image, message,Collapse,Radio,Space,Tooltip } from 'antd';

import { queryDeviceAlarm } from '@/services/deviceAlarm';
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table';


import UpdateForm from '@/pages/alarm/record/components/UpdateForm';



import styles from './index.less';

import { updateDeviceAlarm } from '@/services/deviceAlarm';

export type AlarmProps = {
  display?: string;
  deviceId?:number;
  
};

/**
 * 更新节点
 * @param fields
 */
 const handleUpdate = async (fields: any) => {
  const hide = message.loading('正在保存');
  try {
    await updateDeviceAlarm(fields);
    hide();
    message.success('保存成功');
    return true;
  } catch (error) {
    hide();
    message.error('保存失败请重试！');
    return false;
  }
};

const AlarmTable: React.FC<AlarmProps> = (props: any) => {
  

  const { display ,deviceId} = props




  const [alarmSourceEnumDict, setAlarmSourceEnumDict] = useState({});
  const [alarmCategoryEnumDict, setAlarmCategoryEnumDict] = useState({});
  const [alarmLevelEnumDict, setAlarmLevelEnumDict] = useState({});
  const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
  const [formValues, setFormValues] = useState({});




  const actionRef = useRef<ActionType>();

  const { initialState,setInitialState } = useModel('@@initialState');
  const { dicts } = initialState || {};



  

  useEffect(() => {

    if(dicts){
      setAlarmSourceEnumDict(dicts["5Enum"]);
      setAlarmCategoryEnumDict(dicts["6Enum"]);
      setAlarmLevelEnumDict(dicts["8Enum"])

    }
  }, []);






  


  const columns: ProColumns[] = [

    {
      title: '报警时间',
      dataIndex: 'alarmTime',

    },
    {
      title: '报警类别',
      dataIndex: 'alarmType',
       valueEnum: alarmCategoryEnumDict
    },
    {
      title: '报警级别',
      dataIndex: 'alarmLevel',
      valueEnum:  alarmLevelEnumDict,
    },
    {
      title: '报警数值',
      dataIndex: 'alarmValue',
    }, {
      title: '信息来源',
      dataIndex: 'defectSource',
      valueEnum: alarmSourceEnumDict,
    },
    {
      title: '处置建议',
      search: false,
      width: 240,
      dataIndex: 'handleOpinions',
      render: (text, record) => (
        <Space size="middle"  className={styles.textOverflow}>
          <Tooltip placement="topLeft" title={text} arrowPointAtCenter>
           {text}
          </Tooltip>
        </Space>
       ),
    },
  
    {
      title: '处置状态',
      dataIndex: 'state',
      valueEnum: {0:'未处置',1:'已处置'}
    },
    {
      title: '报警处置信息',
      dataIndex: 'alarmAnswer',

    },



    {
      title: '操作',
      dataIndex: 'state',
      valueType: 'option',
      render: (text, record, _, action) => (
        <>
       {record.state==0?
        <a onClick={async () => {
            handleUpdateModalVisible(true);
            setFormValues(record);
          }}
        >处置</a>:null} 
        </>
      )
    },
  ];



  return (
    <div>
       {deviceId?
          <ProTable
            style={{display:display}}
                  headerTitle=""
                  className={styles.alarmTable}
                  actionRef={actionRef}
                  rowKey="id"
                  search={false}
                  request={(tableParams) => queryDeviceAlarm({ ...tableParams, ...{ deviceId:deviceId } })}
                  columns={columns}
                  options={false}
                  pagination={{
                    defaultCurrent: 1,
                    defaultPageSize: 10,
                  }}
              />
           :      <ProTable
                 style={{display:display}}
                 headerTitle=""
                 rowKey="id"
                 search={false}
                 dataSource={[]}
                 columns={columns}
                 options={false}
                />
                }
      {formValues && Object.keys(formValues).length ? (
        <UpdateForm
          onSubmit={async (value) => {
            const success = await handleUpdate(value);
            if (success) {
              handleUpdateModalVisible(false);
              setFormValues({});
              if (actionRef.current) {
                actionRef.current.reload();
              }
            }
          }}
          onCancel={() => {
            handleUpdateModalVisible(false);
            setFormValues({});
          }}
          updateModalVisible={updateModalVisible}
          values={formValues}
      
        />
      ) : null}
       
    </div>
  );
};

export default AlarmTable;